/*
 ******************************************************************************
 * Parts of this code sample are licensed under Apache License, Version 2.0   *
 * Copyright (c) 2009, Android Open Handset Alliance. All rights reserved.    *
 *																			  *																			*
 * Except as noted, this code sample is offered under a modified BSD license. *
 * Copyright (C) 2010, Motorola Mobility, Inc. All rights reserved.           *
 * 																			  *
 * For more details, see MOTODEV_Studio_for_Android_LicenseNotices.pdf        * 
 * in your installation folder.                                               *
 ******************************************************************************
 */
package dk.lector.providers;

import java.util.HashMap;

import dk.lector.R;
import dk.lector.datasource.DBhelper;


import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;

public class CONFIGContentProvider extends ContentProvider {

	private DBhelper dbHelper;
	private static HashMap<String, String> CONFIG_PROJECTION_MAP;
	private static final String TABLE_NAME = "config";
	private static final String AUTHORITY = "dk.lector.providers.CONFIGContentProvider";

	public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
			+ "/" + TABLE_NAME);
	public static final Uri _ID_FIELD_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + TABLE_NAME.toLowerCase());
	public static final Uri SERVER_URL_FIELD_CONTENT_URI = Uri
			.parse("content://" + AUTHORITY + "/" + TABLE_NAME.toLowerCase()
					+ "/server_url");
	public static final Uri SSL_FIELD_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + TABLE_NAME.toLowerCase() + "/ssl");
	public static final Uri MODULUS_FIELD_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + TABLE_NAME.toLowerCase() + "/modulus");
	public static final Uri EXPONENT_FIELD_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + TABLE_NAME.toLowerCase() + "/exponent");
	public static final Uri DOMAIN_FIELD_CONTENT_URI = Uri.parse("content://"
			+ AUTHORITY + "/" + TABLE_NAME.toLowerCase() + "/domain");
	public static final Uri TIMETOKEEP_DOCS_FIELD_CONTENT_URI = Uri
			.parse("content://" + AUTHORITY + "/" + TABLE_NAME.toLowerCase()
					+ "/timetokeep_docs");
	public static final Uri TIMETOKEEP_USER_FIELD_CONTENT_URI = Uri
			.parse("content://" + AUTHORITY + "/" + TABLE_NAME.toLowerCase()
					+ "/timetokeep_user");
	public static final Uri COMPANY_NAME_FIELD_CONTENT_URI = Uri
			.parse("content://" + AUTHORITY + "/" + TABLE_NAME.toLowerCase()
					+ "/company_name");

	public static final String DEFAULT_SORT_ORDER = "_id ASC";

	private static final UriMatcher URL_MATCHER;

	private static final int CONFIG = 1;
	private static final int CONFIG__ID = 2;
	private static final int CONFIG_SERVER_URL = 3;
	private static final int CONFIG_SSL = 4;
	private static final int CONFIG_MODULUS = 5;
	private static final int CONFIG_EXPONENT = 6;
	private static final int CONFIG_DOMAIN = 7;
	private static final int CONFIG_TIMETOKEEP_DOCS = 8;
	private static final int CONFIG_TIMETOKEEP_USER = 9;
	private static final int CONFIG_COMPANY_NAME = 10;

	// Content values keys (using column names)
	public static final String _ID = "_id";
	public static final String SERVER_URL = "server_url";
	public static final String MODULUS = "modulus";
	public static final String EXPONENT = "exponent";
	public static final String DOMAIN = "domain";
	public static final String TIMETOKEEP_DOCS = "timetokeep_docs";
	public static final String TIMETOKEEP_USER = "timetokeep_user";
	public static final String COMPANY_NAME = "company_name";
	public static final String SSL = "ssl";
	public static final String[] PROJECTION = new String[]{SERVER_URL ,MODULUS,EXPONENT,DOMAIN,TIMETOKEEP_DOCS,TIMETOKEEP_USER,TIMETOKEEP_USER,COMPANY_NAME,SSL};

	public boolean onCreate() {
		dbHelper = new DBhelper(getContext(), true);
		return (dbHelper == null) ? false : true;
	}

	public Cursor query(Uri url, String[] projection, String selection,
			String[] selectionArgs, String sort) {
		SQLiteDatabase mDB = dbHelper.getReadableDatabase();
		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		switch (URL_MATCHER.match(url)) {
		case CONFIG:
			qb.setTables(TABLE_NAME);
			qb.setProjectionMap(CONFIG_PROJECTION_MAP);
			break;
		case CONFIG__ID:
			qb.setTables(TABLE_NAME);
			qb.appendWhere("_id=" + url.getPathSegments().get(1));
			break;
		case CONFIG_SERVER_URL:
			qb.setTables(TABLE_NAME);
			qb.appendWhere("server_url='" + url.getPathSegments().get(2) + "'");
			break;
		case CONFIG_SSL:
			qb.setTables(TABLE_NAME);
			qb.appendWhere("ssl='" + url.getPathSegments().get(2) + "'");
			break;
		case CONFIG_MODULUS:
			qb.setTables(TABLE_NAME);
			qb.appendWhere("modulus='" + url.getPathSegments().get(2) + "'");
			break;
		case CONFIG_EXPONENT:
			qb.setTables(TABLE_NAME);
			qb.appendWhere("exponent='" + url.getPathSegments().get(2) + "'");
			break;
		case CONFIG_DOMAIN:
			qb.setTables(TABLE_NAME);
			qb.appendWhere("domain='" + url.getPathSegments().get(2) + "'");
			break;
		case CONFIG_TIMETOKEEP_DOCS:
			qb.setTables(TABLE_NAME);
			qb.appendWhere("timetokeep_docs='" + url.getPathSegments().get(2)
					+ "'");
			break;
		case CONFIG_TIMETOKEEP_USER:
			qb.setTables(TABLE_NAME);
			qb.appendWhere("timetokeep_user='" + url.getPathSegments().get(2)
					+ "'");
			break;
		case CONFIG_COMPANY_NAME:
			qb.setTables(TABLE_NAME);
			qb.appendWhere("company_name='" + url.getPathSegments().get(2)
					+ "'");
			break;

		default:
			throw new IllegalArgumentException("Unknown URL " + url);
		}
		String orderBy = "";
		if (TextUtils.isEmpty(sort)) {
			orderBy = DEFAULT_SORT_ORDER;
		} else {
			orderBy = sort;
		}
		Cursor c = qb.query(mDB, projection, selection, selectionArgs, null,
				null, orderBy);
		c.setNotificationUri(getContext().getContentResolver(), url);
		return c;
	}

	public String getType(Uri url) {
		switch (URL_MATCHER.match(url)) {
		case CONFIG:
			return "vnd.android.cursor.dir/vnd.com.tsmobile.config";
		case CONFIG__ID:
			return "vnd.android.cursor.item/vnd.com.tsmobile.config";
		case CONFIG_SERVER_URL:
			return "vnd.android.cursor.item/vnd.com.tsmobile.config";
		case CONFIG_SSL:
			return "vnd.android.cursor.item/vnd.com.tsmobile.config";
		case CONFIG_MODULUS:
			return "vnd.android.cursor.item/vnd.com.tsmobile.config";
		case CONFIG_EXPONENT:
			return "vnd.android.cursor.item/vnd.com.tsmobile.config";
		case CONFIG_DOMAIN:
			return "vnd.android.cursor.item/vnd.com.tsmobile.config";
		case CONFIG_TIMETOKEEP_DOCS:
			return "vnd.android.cursor.item/vnd.com.tsmobile.config";
		case CONFIG_TIMETOKEEP_USER:
			return "vnd.android.cursor.item/vnd.com.tsmobile.config";
		case CONFIG_COMPANY_NAME:
			return "vnd.android.cursor.item/vnd.com.tsmobile.config";

		default:
			throw new IllegalArgumentException("Unknown URL " + url);
		}
	}

	public Uri insert(Uri url, ContentValues initialValues) {
		SQLiteDatabase mDB = dbHelper.getWritableDatabase();
		long rowID;
		ContentValues values;
		if (initialValues != null) {
			values = new ContentValues(initialValues);
		} else {
			values = new ContentValues();
		}
		if (URL_MATCHER.match(url) != CONFIG) {
			throw new IllegalArgumentException("Unknown URL " + url);
		}

		rowID = mDB.insert("config", "config", values);
		if (rowID > 0) {
			Uri uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
			getContext().getContentResolver().notifyChange(uri, null);
			return uri;
		}
		throw new SQLException("Failed to insert row into " + url);
	}

	public int delete(Uri url, String where, String[] whereArgs) {
		SQLiteDatabase mDB = dbHelper.getWritableDatabase();
		int count;
		String segment = "";
		switch (URL_MATCHER.match(url)) {
		case CONFIG:
			count = mDB.delete(TABLE_NAME, where, whereArgs);
			break;
		case CONFIG__ID:
			segment = url.getPathSegments().get(1);
			count = mDB.delete(TABLE_NAME,
					"_id="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case CONFIG_SERVER_URL:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.delete(TABLE_NAME,
					"server_url="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case CONFIG_SSL:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.delete(TABLE_NAME,
					"ssl="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case CONFIG_MODULUS:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.delete(TABLE_NAME,
					"modulus="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case CONFIG_EXPONENT:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.delete(TABLE_NAME,
					"exponent="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case CONFIG_DOMAIN:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.delete(TABLE_NAME,
					"domain="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case CONFIG_TIMETOKEEP_DOCS:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.delete(TABLE_NAME,
					"timetokeep_docs="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case CONFIG_TIMETOKEEP_USER:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.delete(TABLE_NAME,
					"timetokeep_user="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case CONFIG_COMPANY_NAME:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.delete(TABLE_NAME,
					"company_name="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;

		default:
			throw new IllegalArgumentException("Unknown URL " + url);
		}
		getContext().getContentResolver().notifyChange(url, null);
		return count;
	}

	public int update(Uri url, ContentValues values, String where,
			String[] whereArgs) {
		SQLiteDatabase mDB = dbHelper.getWritableDatabase();
		int count;
		String segment = "";
		switch (URL_MATCHER.match(url)) {
		case CONFIG:
			count = mDB.update(TABLE_NAME, values, where, whereArgs);
			break;
		case CONFIG__ID:
			segment = url.getPathSegments().get(1);
			count = mDB.update(TABLE_NAME, values,
					"_id="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case CONFIG_SERVER_URL:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.update(TABLE_NAME, values,
					"server_url="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case CONFIG_SSL:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.update(TABLE_NAME, values,
					"ssl="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case CONFIG_MODULUS:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.update(TABLE_NAME, values,
					"modulus="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case CONFIG_EXPONENT:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.update(TABLE_NAME, values,
					"exponent="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case CONFIG_DOMAIN:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.update(TABLE_NAME, values,
					"domain="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case CONFIG_TIMETOKEEP_DOCS:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.update(TABLE_NAME, values,
					"timetokeep_docs="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case CONFIG_TIMETOKEEP_USER:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.update(TABLE_NAME, values,
					"timetokeep_user="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;
		case CONFIG_COMPANY_NAME:
			segment = "'" + url.getPathSegments().get(2) + "'";
			count = mDB.update(TABLE_NAME, values,
					"company_name="
							+ segment
							+ (!TextUtils.isEmpty(where) ? " AND (" + where
									+ ')' : ""), whereArgs);
			break;

		default:
			throw new IllegalArgumentException("Unknown URL " + url);
		}
		getContext().getContentResolver().notifyChange(url, null);
		return count;
	}

	static {
		URL_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
		URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase(), CONFIG);
		URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase() + "/#",
				CONFIG__ID);
		URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase() + "/server_url"
				+ "/*", CONFIG_SERVER_URL);
		URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase() + "/ssl" + "/*",
				CONFIG_SSL);
		URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase() + "/modulus"
				+ "/*", CONFIG_MODULUS);
		URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase() + "/exponent"
				+ "/*", CONFIG_EXPONENT);
		URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase() + "/domain"
				+ "/*", CONFIG_DOMAIN);
		URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase()
				+ "/timetokeep_docs" + "/*", CONFIG_TIMETOKEEP_DOCS);
		URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase()
				+ "/timetokeep_user" + "/*", CONFIG_TIMETOKEEP_USER);
		URL_MATCHER.addURI(AUTHORITY, TABLE_NAME.toLowerCase()
				+ "/company_name" + "/*", CONFIG_COMPANY_NAME);

		CONFIG_PROJECTION_MAP = new HashMap<String, String>();
		CONFIG_PROJECTION_MAP.put(_ID, "_id");
		CONFIG_PROJECTION_MAP.put(SERVER_URL, "server_url");
		CONFIG_PROJECTION_MAP.put(SSL, "ssl");
		CONFIG_PROJECTION_MAP.put(MODULUS, "modulus");
		CONFIG_PROJECTION_MAP.put(EXPONENT, "exponent");
		CONFIG_PROJECTION_MAP.put(DOMAIN, "domain");
		CONFIG_PROJECTION_MAP.put(TIMETOKEEP_DOCS, "timetokeep_docs");
		CONFIG_PROJECTION_MAP.put(TIMETOKEEP_USER, "timetokeep_user");
		CONFIG_PROJECTION_MAP.put(COMPANY_NAME, "company_name");

	}
}
